package com.stedu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.OrderInsert;
import com.stedu.bean.Orders;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Value("10")
    private  Integer pageSize;

    @PostMapping
    public RespBean insert(@RequestBody OrderInsert orderInsert) throws SteduException {
        Orders orders= new Orders();
        orders.setCid(orderInsert.getCid());
        orders.setId(orderInsert.getId());
        orders.setDays(orderInsert.getDays());
        if (ordersService.insert(orders)) {
            return RespBean.ok("租车成功~~~");
        } else {
            return RespBean.error("租车失败，请重新租车");
        }

    }
    //@DeleteMapping("/{cid}")
    //public RespBean delete(@PathVariable("cid") int cid ) throws SteduException {
    //    //获取请求参数
    //
    //
    //    if (ordersService.delete(cid)) {
    //        return  RespBean.ok("删除成功~~~");
    //    } else {
    //        return RespBean.error("删除失败，请重新删除");
    //    }
    //}
    @PutMapping("/{id}")
    public RespBean update(@PathVariable("id") Integer id) throws SteduException {
        Orders orders = ordersService.selectByOid(id);
        orders.setOstate("3");
        if (ordersService.update(orders)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return   RespBean.error("修改失败，请重新修改");
        }
    }
    @GetMapping("/{oid}")
    public RespBean selectByOid(@PathVariable("oid") int oid) {
        Orders c = ordersService.selectByOid(oid);
        return RespBean.ok("", c);
    }

    @GetMapping
    public RespBean selectOstate(Integer pageNum,String ostate){
        if (pageNum == null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum ,pageSize);
        List<Orders> ordersList=ordersService.selectostate(ostate);
        PageInfo<Orders> pageInfo=new PageInfo<>(ordersList,5);
        return  RespBean.ok("查询成功", pageInfo);
    }

    @GetMapping("/usercars")
    public RespBean selectID(Integer pageNum,int id){
        if (pageNum == null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum ,pageSize);
        List<Orders> ordersList=ordersService.selectId(id);
        PageInfo<Orders> pageInfo=new PageInfo<>(ordersList,5);
        return  RespBean.ok("查询成功", pageInfo);
    }



    
}
